<?php
include_once dirname(__FILE__)."/../../model/config.php";
		$_piece_joint=new piece_joint();
if(isset($_POST["operation"])){
if($_POST["operation"]=="INSERT"){
		if(isset($_POST["id_piece"])){$_piece_joint->set_id_piece($_POST["id_piece"]);}
		if(isset($_POST["id_mail"])){$_piece_joint->set_id_mail($_POST["id_mail"]);}
		if(isset($_POST["piece"])){$_piece_joint->set_piece($_POST["piece"]);}
		if(isset($_POST["description"])){$_piece_joint->set_description($_POST["description"]);}
		$res=$_piece_joint->INSERT(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}
elseif($_POST["operation"]=="DELETE"){
		if(isset($_POST["id_piece"])){
		$_piece_joint->set_id_piece($_POST["id_piece"]);
		$res=$_piece_joint->DELETE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
		}
}
elseif($_POST["operation"]=="UPDATE"){
		if(isset($_POST["id_piece"])){$_piece_joint->set_id_piece($_POST["id_piece"]);}
		if(isset($_POST["id_mail"])){$_piece_joint->set_id_mail($_POST["id_mail"]);}
		if(isset($_POST["piece"])){$_piece_joint->set_piece($_POST["piece"]);}
		if(isset($_POST["description"])){$_piece_joint->set_description($_POST["description"]);}
		$res=$_piece_joint->UPDATE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}

}
else{
	$constraint='';$limit=0;$start=0;$pagination='';
	if(isset($_POST["id_piece"]) && !empty($_POST["id_piece"])){
		$constraint.=" AND id_piece='".$_POST["id_piece"]."'";
	}

	if(isset($_POST["id_mail"]) && !empty($_POST["id_mail"])){
		$constraint.=" AND id_mail='".$_POST["id_mail"]."'";
	}

	if(isset($_POST["piece"]) && !empty($_POST["piece"])){
		$constraint.=" AND piece='".$_POST["piece"]."'";
	}

	if(isset($_POST["description"]) && !empty($_POST["description"])){
		$constraint.=" AND description='".$_POST["description"]."'";
	}

	if(isset($_POST['limit'])){$limit=$_POST['limit'];}
	if(isset($_POST['start'])){$start=$_POST['start'];}
	if($limit>0){
		$pagination.=" LIMIT  $start,$limit"; 
}
		$res=$_piece_joint->SELECT($constraint,$pagination); 

		 $data=array();
		  while($row=$_piece_joint->fetch_object()){
		  	$data[]=$row;
	  	  }
  	 	 
		
	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('totalcount' => count($data),'result' => $data ));
	  	
	  }}
?>